package com.shenzhen.horselive.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ViewAnimator;

import com.flqy.baselibrary.utils.NetworkUtil;
import com.flqy.baselibrary.utils.ScreenUtil;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.api.ApiException;

public class ContentViewHolder extends ViewAnimator {

    public static final int LOADING = 0;
    public static final int RETRY = 1;
    public static final int NO_DATA = 2;
    public static final int CONTENT = 3;
    protected Button retry;
    private boolean retryInNoData = false;
    protected TextView noData;
    protected TextView errorPromptView;
    protected ProgressBar progressBar;
    private static final int CHILD_SIZE = 4;
    private String noDataHint;
    private CharSequence defaultErrorPrompt;
    private ImageView icon;
    private ImageView noDataIcon;
    private int avaliableHeight;

    public ContentViewHolder(Context context, boolean scrollable) {
        super(context);
        initView(context, scrollable);
    }

    //    private ViewCo
    public ContentViewHolder(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, false);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    public void showNoData() {
        if (retryInNoData) {
            if (errorPromptView != null)
                errorPromptView.setText(noDataHint);
            setDisplayedChild(RETRY);
        } else {
            setDisplayedChild(NO_DATA);
        }
    }

    protected void initView(Context context, boolean scrollable) {
        View v = inflateLayout(scrollable);
        if (scrollable) {
            View dividerEmpty1 = v.findViewById(R.id.divider_empty_1);
            View dividerEmpty2 = v.findViewById(R.id.divider_empty_1);
            View dividerNetError1 = v.findViewById(R.id.divider_net_error_1);
            View dividerNetError2 = v.findViewById(R.id.divider_net_error_2);
            View dividerNetError3 = v.findViewById(R.id.divider_net_error_3);
            View progressLayout=v.findViewById(R.id.progress_layout);
            int screenHeight = ScreenUtil.getScreenHeight();
            progressLayout.setMinimumHeight((int) (screenHeight/2f));
            int itemHeight = screenHeight / 95;
            dividerEmpty1.setMinimumHeight(itemHeight * 6);
            dividerEmpty2.setMinimumHeight(itemHeight* 3);
            dividerNetError1.setMinimumHeight(itemHeight);
            dividerNetError2.setMinimumHeight((int) (itemHeight * 1.5f));
            dividerNetError3.setMinimumHeight(itemHeight);
        }

        retry = (Button) v.findViewById(R.id.retry_btn);
        noData = (TextView) v.findViewById(R.id.no_data);
        errorPromptView = (TextView) findViewById(R.id.error_prompt_view);
        progressBar = (ProgressBar) v.findViewById(R.id.loading_progress);
        noDataIcon = v.findViewById(R.id.no_data_icon);
        icon = v.findViewById(R.id.icon);
        defaultErrorPrompt = errorPromptView.getText();
//        setMeasureAllChildren(false);
    }

    public void setContent(View content) {
        if (getChildCount() == CHILD_SIZE) {
            removeViewAt(CONTENT);
        }
        ViewGroup parent = (ViewGroup) content.getParent();
        ViewGroup.LayoutParams contentLayoutParams = content.getLayoutParams();
        int index = parent.indexOfChild(content);
        parent.removeView(content);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(content, CONTENT, params);
        parent.addView(this, index, contentLayoutParams);
    }

    public void setEmptyView(View emptyView) {
        removeViewAt(NO_DATA);
        if (emptyView.getParent() != null) {
            throw new IllegalArgumentException("child already has parent");
        }
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(emptyView, NO_DATA, params);
    }

    protected View inflateLayout(boolean scrollable) {
        return inflate(getContext(), scrollable ? R.layout.scrollable_content_view_holder : R.layout.content_view_holder, this);
    }

    public void showContent() {
        setDisplayedChild(CONTENT);
        progressBar.invalidate();
    }

    public void showLoading() {
        progressBar.setVisibility(View.VISIBLE);
        setDisplayedChild(LOADING);
    }


    public void showRetry(Throwable e) {
        setDisplayedChild(RETRY);
        if (NetworkUtil.isNetworkAvailable()) {
            if (e != null && e instanceof ApiException) {
                setErrorPrompt(e.getMessage());
            } else {
                setErrorPrompt(R.string.common_error_msg);
            }
            icon.setImageResource(R.drawable.ic_no_network);
        } else {
            setErrorPrompt(R.string.network_unavailable);
            icon.setImageResource(R.drawable.ic_no_network);
        }
    }

    public void showEmpty() {
        setDisplayedChild(NO_DATA);
    }

    public void showEmpty(String noDataPrompt) {
        setDisplayedChild(NO_DATA);
        setNoDataPrompt(noDataPrompt);
    }

    public void setNodataIcon(int resId) {
        noDataIcon.setImageResource(resId);
    }

    public void setRetryListener(final OnClickListener listener) {
        retry.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(view);
            }
        });
    }


    public void setDefaultEmptyImage(int resId) {
        noData.setBackgroundResource(resId);
    }

    public void setNoDataPrompt(String noDataPrompt) {
        noData.setText(noDataPrompt);
    }

    public void setErrorPrompt(int resId) {
        errorPromptView.setText(resId);
    }

    public void setErrorPrompt(CharSequence errorPrompt) {
        errorPromptView.setText(errorPrompt);
    }

    public int getCurrentViewIndex() {
        return indexOfChild(getCurrentView());
    }

    public void showQuietLoading() {
        setDisplayedChild(LOADING);
        progressBar.setVisibility(View.GONE);
    }

}